#include"preCmpH.h"
#include"UIEventManager.h"

BEGIN_NAMESPACE(ui_manager)
BEGIN_NAMESPACE(ui_events)
//=====================================
// Constructors, Destructor, Operators
//=====================================

UIEventManager::UIEventManager(){}
UIEventManager::~UIEventManager(){}
//=====================================
// Functionality
//=====================================

void UIEventManager::Clear()
{
	events_.clear();
}
void UIEventManager::Polling()
{
	if(timer_.hasTicked()){
		UIEvent e;
		e.setMsgFlag(UIEvent::FLAG_TICK);
		e.setTickMsg(true,timer_.getInterval(),0);
		events_.push_front(e);
	}
	if(inputManager_->hasKeysStateChanged()){
		UIEvent e;
		e.setMsgFlag(UIEvent::FLAG_KEYBOARD);
		e.setKeyboardMsg(
			inputManager_->GetLastKeyAction(),
			inputManager_->GetLastKey());
		events_.push_front(e);
	}
	if(inputManager_->hasButtonsStateChanged()){
		UIEvent e;
		e.setMsgFlag(UIEvent::FLAG_MOUSE);
		e.setMouseMsg(
			inputManager_->GetMouseCoordX(),
			inputManager_->GetMouseCoordY(),
			inputManager_->GetMouseAbsCoordX(),
			inputManager_->GetMouseAbsCoordY(),
			inputManager_->GetLastMouseAction(),
			inputManager_->GetLastMouseButton());
		events_.push_front(e);
	} else {
		UIEvent e;
		e.setMsgFlag(UIEvent::FLAG_MOUSE);
		e.setMouseMsg(
			inputManager_->GetMouseCoordX(),
			inputManager_->GetMouseCoordY(),
			inputManager_->GetMouseAbsCoordX(),
			inputManager_->GetMouseAbsCoordY(),
			0, 0);
		events_.push_front(e);
	}
	UIEvent e;
	e.setMsgFlag(UIEvent::FLAG_RENDER);
	e.setRenderMsg(0,0,true);
	events_.push_front(e);
}
//=====================================
// Setters
//=====================================

void UIEventManager::addEvent(UIEvent uievent)
{
	events_.push_front(uievent);
}
//=====================================
// Getters
//=====================================

bool UIEventManager::isEmpty()
{
	if(events_.empty()){
		return true;
	}
	return false;
}
UIEvent UIEventManager::getEvent()
{
	UIEvent tor = events_.back();
	events_.pop_back();
	return tor;
}
ui_input::UIInputManager& UIEventManager::getInputManager()
{
	return *inputManager_;
}
UITimer& UIEventManager::getTimer()
{
	return timer_;
}
UIEventManager& UIEventManager::getInstance()
{
	static UIEventManager* instance = null;
	if(instance == null){
		instance = new UIEventManager();
	}
	return *instance;
}

void UIEventManager::StartUp( UIManager& manager )
{
	inputManager_ = &ui_input::UIInputManager::getInstance();
	inputManager_->StartUp(*this);
}

void UIEventManager::ShutDown()
{
	delete inputManager_;
}

END_NAMESPACE // UI Events
END_NAMESPACE // UI Manager